﻿using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using MyCraft.Minecraft_2D;
using System;

namespace MyCraft
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        MineCraft2D game;
        public static Player pikachu;
        //List<Player> _mobs;
        Alert alert;
        FPSCounter fpscounter;
        public static Microsoft.Xna.Framework.Content.ContentManager manager;
        public delegate void HasXbox(MouseState mouse, KeyboardState keyboard, GameTime gameTime, GraphicsDeviceManager device);
        public static HasXbox _mineCraft2DUpload;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            manager = Content;
            Content.RootDirectory = "Content";

            //this.IsMouseVisible = true;
            this.IsFixedTimeStep = false;
            this.graphics.SynchronizeWithVerticalRetrace = false;
            this.graphics.ApplyChanges();
            this.graphics.PreferredBackBufferWidth = 800;
            this.graphics.PreferredBackBufferHeight = (int)Math.Ceiling(800 / this.graphics.GraphicsDevice.DisplayMode.AspectRatio);
            //this.graphics.ToggleFullScreen();
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            alert = new Alert();
            game = new MineCraft2D(graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight);
            pikachu = new Player();
            fpscounter = new FPSCounter();
            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            Alert.Alerte("Screen", "Screen, {W:" + this.graphics.PreferredBackBufferWidth + ", H:" + this.graphics.PreferredBackBufferHeight + "}", 4000, Color.Blue, null);

            game.LoadContent(Content, out _mineCraft2DUpload);
            pikachu.LoadContent(Content);
            alert.LoadContent(Content);
            fpscounter.LoadContent(Content);
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            MouseState mouse = Mouse.GetState();
            KeyboardState keyboard = Keyboard.GetState();
            pikachu.Update(mouse, keyboard, gameTime);
            _mineCraft2DUpload(mouse, keyboard, gameTime, graphics);
            alert.Update(mouse, gameTime, graphics.GraphicsDevice);
            fpscounter.Update(gameTime);

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            spriteBatch.Begin();
            
            game.Draw(spriteBatch);
            pikachu.Draw(spriteBatch);
            alert.Draw(spriteBatch);
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}
